﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcVehicleInsurance.Models;

namespace MvcVehicleInsurance.Areas.Admin.Controllers
{
    public class AccountController : Controller
    {
        //
        // GET: /Admin/Account/
        private DataClassesVehicleDataContext data = new DataClassesVehicleDataContext();


        public ActionResult Index()
        {
            List<Account> aci = (from ac in data.Accounts select ac).ToList();
            if (ModelState.IsValid)
            {
                return View(aci);
            }
            return View();
        }


        public ActionResult Create()
        {
            Account ac = new Account();
            return View(ac);
        }


        [HttpPost]
        public ActionResult Create(Account acc)
        {
            if(ModelState.IsValid)
            {  
                data.Accounts.InsertOnSubmit(acc);
                data.SubmitChanges();
                RedirectToAction("Index");
            }
            return View();
        }


        public ActionResult Edit()
        {
            Account ac = new Account();
            return View(ac);
        }


        [HttpPost]
        public ActionResult Edit(string id, Account ace)
        {
            Account get = data.Accounts.Where(ac => ac.Acc_ID == id).First();

            get.Acc_Username = ace.Acc_Username;
            get.Acc_Password = ace.Acc_Password;
            get.Acc_FullName = ace.Acc_FullName;
            get.Acc_Gender = ace.Acc_Gender;
            get.Acc_Phone = ace.Acc_Phone;
            get.Acc_Address = ace.Acc_Address;
            get.Acc_Email = ace.Acc_Email;

            data.SubmitChanges();
            RedirectToAction("Index");
            return View();
        }
        
        
        public ActionResult Details(string id)
        {
            Account acd = (from ac in data.Accounts.Where(ac => ac.Acc_ID == id) select ac).First();
            if(ModelState.IsValid)
            {
                return View(acd);
            }
            return View();
        }


        //
        // Search
        public ActionResult Search()
        {
            List<Account> acc = new List<Account>();
            return View(acc);
        }

     

        [HttpPost]
        public ActionResult Search(string Username)
        {
            List<Account> acs = (from ac in data.Accounts.Where(acc=>acc.Acc_Username.Contains(Username)) select ac).ToList();
            return View(acs);
        }
    }
}
